package com.shanjupay.merchant.controller;


import com.shanjupay.merchant.common.util.SecurityUtil;
import com.shanjupay.transaction.api.PayChannelService;
import com.shanjupay.transaction.api.dto.PayChannelDTO;
import com.shanjupay.transaction.api.dto.PayChannelParamDTO;
import com.shanjupay.transaction.api.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(value = "商户平台-渠道和支付参数的相关",tags = "商户平台-渠道和支付参数的相关",description = "商户平台-渠道和支付参数的相关")
public class PlatformParamController {

    @Reference
    PayChannelService payChannelService;

//    @ApiOperation("获取平台服务类型")
//    @GetMapping("/my/platform‐channels")
//    public List<PlatformChannelDTO> queryPlatformChannel(){
//        //代理对象 调用远程服务 里面查询平台所有的服务类型
//        return payChannelService.queryPlatformChannel();
//    }


    @ApiOperation("获取平台服务类型")
    @GetMapping(value="/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel(){
        return payChannelService.queryPlatformChannel();
    }



    /*应用与服务绑定*/
    @ApiOperation("绑定服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(value ="应用id",name = "appId",required = true, dataType = "String",paramType = "path"),
            @ApiImplicitParam(value ="服务类型code",name = "platformChannelCodes",required = true, dataType = "String",paramType = "query")
    })
    @PostMapping(value="/my/apps/{appId}/platform-channels")
    public void bindPlatformForApp(@PathVariable("appId") String appId
            ,@RequestParam("platformChannelCodes") String platformChannelCodes){
        //生产的代理对象调用远程服务
        payChannelService.bindPlatformChannelForApp(appId,platformChannelCodes);
    }

    @ApiOperation("查询应用与服务的绑定状态")
    @GetMapping(value = "/my/merchants/apps/platformchannels")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "platformChannel",value = "绑定的服务",required = true,dataType = "String",paramType = "query")

    })
    public int queryAppBindPlatformChannel(@RequestParam("appId")  String appId,@RequestParam("platformChannel") String platformChannel){

        return payChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }

    /*
    * 根据服务类型进行查询支付渠道列表
    *
    * */
    @ApiOperation("根据服务类型进行查询支付渠道列表")
    @GetMapping(value="/my/pay-channels/platform-channel/{platformChannelCode}")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "platformChannelCode",value = "服务类型码",required = true,
            dataType = "String",paramType = "path")
    )
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable  String platformChannelCode){

        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);

    }
    /*
    * 支付渠道参数配置的controller接口
    *
    * */
    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "PayChannelParamDTO",value = "商户配置支付渠道参数",required = true,dataType = "PayChannelParamDTO",paramType = "body"
            )
    })
    @RequestMapping(value = "/my/pay-channel-params",method = {RequestMethod.POST,RequestMethod.PUT})
    public void createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO){
        //解析令牌里面的用户id
        Long merchantId = SecurityUtil.getMerchantId();
        //把商户id设置配置支付渠道参数
        payChannelParamDTO.setMerchantId(merchantId);
        //进行远程调用交易服务的配置支付渠道参数的服务
        payChannelService.savePayChannelParam(payChannelParamDTO);
    }

    /*
    *
    * 获取支付渠道参数列表
    *
    * */
    @ApiOperation("指定应用指定服务类型下的所有支付渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用id",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "platformChannel",value = "服务类型code",required = true,dataType = "String",paramType = "path")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public  List<PayChannelParamDTO> queryPayChannelParam(@PathVariable  String appId,@PathVariable  String platformChannel){
        return payChannelService.queryPayChannelParamByAppAndPlatform(appId,platformChannel);
    }

    /*
    *
    * 获取具体应用具体服务类型的指定的支付渠道的支付渠道参数参数
    * */

    @ApiOperation("指定应用指定服务的指定支付渠道的 支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用id",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "platformChannel",value = "服务类型code",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "payChannel",value = "支付渠道",required = true,dataType = "String",paramType = "path")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId,@PathVariable String platformChannel,@PathVariable String payChannel){

        return payChannelService.queryParamByAppPlatformAndPayChannel(appId,platformChannel,payChannel);
    }



}
